"""
1-10个类别信息熵的演示
"""
import math
import numpy as np
import matplotlib.pyplot as plt

class_num = 10

# 计算熵值方法
def cal_entropy(n):
    p = 1.0 / n # 概率
    total_entropy = 0.0 # 总熵值

    for i in range(n):
        p_i = p * math.log2(p)
        total_entropy += p_i
    return - total_entropy

entropies = []
for i in range(1,class_num+1):
    entropy = cal_entropy(i)
    entropies.append(entropy)

plt.plot(np.arange(1,class_num+1),entropies,color='orangered')
plt.show()
